package xing.domain.isv.suite;

import com.alibaba.fastjson.JSON;
import xing.domain.config.WebConsts;
import xing.infrastructure.util.CookieUtil;
import xing.infrastructure.util.SymmetricEncoder;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.net.URLDecoder;

/*******************************************************************************************
 * 类描述：  
 *
 * @author: dustin
 * @date： 2017-8-31 15:16
 * @Description:
 * @version 1.0.0
 *
 *
 * Version    Date       ModifiedBy                 Content  
 * -------- ---------    ----------         ------------------------  
 * 1.0.0     2017-8-31    Administrator                            
 *******************************************************************************************
 */
public class LoginUser implements Serializable {
    private String corpId;
    private String userId; //员工在企业内的UserID
    private Boolean isSys; //是否是管理员
    private Integer sysLevel; //级别，三种取值。0:非管理员 1：普通管理员 2：超级管理员
    private String deviceId; //手机设备号,由钉钉在安装时随机产生

    public LoginUser() { }


    public LoginUser(String corpId, String userId, Boolean isSys, Integer sysLevel, String deviceId) {
        this.corpId = corpId;
        this.userId = userId;
        this.isSys = isSys;
        this.sysLevel = sysLevel;
        this.deviceId = deviceId;
    }

    public String getCorpId() {
        return corpId;
    }

    public void setCorpId(String corpId) {
        this.corpId = corpId;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public Boolean getIsSys() {
        return isSys;
    }

    public void setIsSys(Boolean isSys) {
        this.isSys = isSys;
    }

    public Integer getSysLevel() {
        return sysLevel;
    }

    public void setSysLevel(Integer sysLevel) {
        this.sysLevel = sysLevel;
    }

    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }


    public static LoginUser getCurrentUser(HttpServletRequest request) throws Exception{
        Cookie c= CookieUtil.getCookieByName(request, WebConsts.CurrentUser);
        if(c==null){
            return null;
        }
        String urlDecodeJson= URLDecoder.decode(c.getValue(),"utf-8");
        String userJson= SymmetricEncoder.AESDecode(urlDecodeJson);
        LoginUser u= JSON.parseObject(userJson,LoginUser.class);
        return u;
    }

    @Override
    public String toString() {
        return "LoginUser{" +
                "corpId='" + corpId + '\'' +
                ", userId='" + userId + '\'' +
                ", isSys=" + isSys +
                ", sysLevel=" + sysLevel +
                ", deviceId='" + deviceId + '\'' +
                '}';
    }
}
